Skip to content

FlagSelectionWidget

Base classes

Name Children Inherits
GroupBox
prettyqt.widgets.groupbox
Group box frame with a title.

⋔ Inheritance diagram

graph TD
  3231056394["custom_widgets.FlagSelectionWidget"]
  595380733["widgets.GroupBox"]
  829357528["widgets.WidgetMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  625385617["gui.PaintDeviceMixin"]
  2026038775["QtWidgets.QGroupBox"]
  3620195106["QtWidgets.QWidget"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  673956333["QtGui.QPaintDevice"]
  595380733 --> 3231056394
  829357528 --> 595380733
  3227531497 --> 829357528
  2751680898 --> 3227531497
  625385617 --> 829357528
  2751680898 --> 625385617
  2751680898 --> 829357528
  2026038775 --> 595380733
  3620195106 --> 2026038775
  3984060476 --> 3620195106
  37984305 --> 3984060476
  2751680898 --> 37984305
  673956333 --> 3620195106
  37984305 --> 673956333

🛈 DocStrings

Bases: GroupBox

Source code in prettyqt/custom_widgets/editors/flagselectionwidget.py
class FlagSelectionWidget(widgets.GroupBox):
    value_changed = core.Signal(int)

    def __init__(
        self,
        label: str = "",
        layout: Literal["horizontal", "vertical"] = "vertical",
        object_name: str = "flag_selection_widget",
        **kwargs,
    ):
        super().__init__(title=label, object_name=object_name, **kwargs)
        self.set_layout(layout)
        self.buttons: dict[widgets.CheckBox, int] = {}

    def __iter__(self) -> Iterator[tuple[widgets.CheckBox, int]]:
        return iter(self.buttons.items())

    def add_items(self, items: Iterable | Mapping):
        if isinstance(items, Mapping):
            for k, v in items.items():
                self.add(v, k)
        else:
            for i in items:
                if isinstance(i, Iterable):
                    self.add(*i)
                else:
                    msg = "Invalid item type"
                    raise TypeError(msg)

    def add(self, title: str, flag: int):
        checkbox = widgets.CheckBox(title, toggled=self.update_choice)
        self.buttons[checkbox] = flag
        self.box.add(checkbox)

    def current_choice(self) -> int:
        ret_val = 0
        for btn, flag in self.buttons.items():
            if btn.isChecked():
                ret_val |= flag
        return int(ret_val)

    @core.Slot(bool)
    def update_choice(self, checked: bool):
        choice = self.current_choice()
        self.value_changed.emit(choice)

    def set_value(self, value: int):
        for btn, flag in self.buttons.items():
            btn.setChecked(bool(value & flag))

    def get_value(self) -> int:
        return self.current_choice()

:fa-brands-github: Show source on GitHub